package com.moyq5.permit.center.feign.server.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.Page;
import com.moyq5.mvc.framework.general.Paging;
import com.moyq5.mvc.framework.general.Sorting;
import com.moyq5.mvc.framework.jackson.result.ListResult;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.PageResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.utils.EntityUtils;
import com.moyq5.permit.center.bean.entity.SysPermit;
import com.moyq5.permit.center.bean.search.SysPermit4Search;
import com.moyq5.permit.center.core.service.SysPermitService;
import com.moyq5.permit.center.feign.server.BaseController;
import com.moyq5.permit.center.feign.server.jackson.filter.SysPermitFilter;


/**
 * @author Moyq5’s Tools
 * @since 2019-07-09 18:05:18
 */
@RestController
@RequestMapping("/permit")
public class SysPermitController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(SysPermitController.class);
	
	@Autowired
	private SysPermitService permitService;
	
	/**
	 * 根据ID获取对象
	 * @author Moyq5’s Tools
	 * @param permitId
	 * @param filter
	 * @return
	 */
	@PostMapping("/oneById")
	@ResponseBody
	public MappingJacksonValue oneById(@RequestBody Long permitId, String filter) {
		log.debug(">>>>oneById: permitId={},filter={}", permitId, filter);
		SysPermit permit = permitService.one(permitId);
		ObjectResult<SysPermit> objRst = new ObjectResult<SysPermit>();
		objRst.setData(permit);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(SysPermitFilter.get(filter));
		return value;
	}
	
	/**
	 * 查询记录数
	 * @author Moyq5’s Tools
	 * @param permit4s
	 * @return
	 */
	@PostMapping("/count")
	@ResponseBody
	public ObjectResult<Long> count(@RequestBody Body<SysPermit4Search, String> permit4s) {
		log.debug(">>>>count: ");
		Long count = permitService.count(permit4s.getEntity(), permit4s.getCustom());
		ObjectResult<Long> objRst = new ObjectResult<Long>();
		objRst.setData(count);
		objRst.setOk();
		return objRst;
	}
	
	/**
	 * 查询单个对象
	 * @author Moyq5’s Tools
	 * @param permit4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/one")
	@ResponseBody
	public MappingJacksonValue one(@RequestBody Body<SysPermit4Search, Sorting> permit4s, String filter) {
		log.debug(">>>>one: filter={}", filter);
		SysPermit permit = permitService.one(permit4s.getEntity(), permit4s.getCustom());
		ObjectResult<SysPermit> objRst = new ObjectResult<SysPermit>();
		objRst.setData(permit);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(SysPermitFilter.get(filter));
		return value;
	}
	
	/**
	 * 列表查询
	 * @author Moyq5’s Tools
	 * @param permit4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/list")
	@ResponseBody
	public MappingJacksonValue list(@RequestBody Body<SysPermit4Search, Sorting> permit4s, String filter) {
		log.debug(">>>>list: filter={}", filter);
		List<SysPermit> list = permitService.list(permit4s.getEntity(), permit4s.getCustom());
		ListResult<SysPermit> listRst = new ListResult<SysPermit>();
		listRst.setData(list);
		listRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(listRst);
		value.setFilters(SysPermitFilter.get(filter));
		return value;
	}
	
	/**
	 * 分页查询
	 * @author Moyq5’s Tools
	 * @param permit4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/page")
	@ResponseBody
	public MappingJacksonValue page(@RequestBody Body<SysPermit4Search, Paging> permit4s, String filter) {
		log.debug(">>>>page: filter={}", filter);
		Page<SysPermit> page = permitService.page(permit4s.getEntity(), permit4s.getCustom());
		PageResult<SysPermit> pageRst = new PageResult<SysPermit>();
		pageRst.setData(page);
		pageRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(pageRst);
		value.setFilters(SysPermitFilter.get(filter));
		return value;
	}
	
	/**
	 * 添加
	 * @author Moyq5’s Tools
	 * @param from
	 * @return
	 */
	@PutMapping
	@ResponseBody
	public ObjectResult<Long> create(@RequestBody SysPermit from) {
		log.debug(">>>>add: SysPermit");
		SysPermit permit = permitService.persist(from);
		ObjectResult<Long> result = new ObjectResult<Long>();
		result.setData(permit.getPermitId());
		result.setOk();
		return result;
	}
	
	/**
	 * 更新
	 * @author Moyq5’s Tools
	 * @param form
	 * @return
	 */
	@PatchMapping
	@ResponseBody
	public StatusResult update(@RequestBody SysPermit form) {
		log.debug(">>>>update: SysPermit");
		SysPermit permit = permitService.one(form.getPermitId());
		EntityUtils.combine(form, permit);
		permitService.merge(permit);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	/**
	 * 删除
	 * @author Moyq5’s Tools
	 * @param permitId
	 * @return
	 */
	@DeleteMapping
	@ResponseBody
	public StatusResult delete(@RequestBody Long permitId) {
		log.debug(">>>>delete: permitId={}", permitId);
		permitService.remove(permitId);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
}
